package com.kingcore.director.note.bean;
 //注意，这个方法对于图片而言，会保存为静态图片  
 //代码是可以直接调用了的  
 /* 
  //写入序列化文件 
  WriteOrReadRecordFile worrf = new  WriteOrReadRecordFile(你的JTextPane实例 , "2011-5-28" , "10000" , "F://"); 
  worrf.writeToFile(); 
  //读取序列化文件。默认读取第n个文件。 
  WriteOrReadRecordFile worrf = new  WriteOrReadRecordFile(null , "2011-5-28" , "10000" , "F://"); 
  你的JTextPane实例.setStyledDocument(worrf.readFromFile()); 
  */  
 import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Calendar;

import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import javax.swing.text.StyledDocument;

 public class WriteOrReadNoteFile {  
     // 这个obj，其实对于这个模块就是用来接收SJTextPane的。  
     private JTextPane m_Obj;  
     private String m_Str_Date;  
     private String m_Str_ID;  
     private String m_filePath;  
     public WriteOrReadNoteFile(JTextPane obj, String ID,  
             String filePath) {  
         m_Obj = obj;  
         m_Str_Date = Calendar.getInstance().toString();  
         m_Str_ID = ID;  
         m_filePath = filePath;  
     }  
     // 用来接收文件的路径(含文件名)。  
     public boolean writeToFile() {  
         StringBuffer sb = new StringBuffer("");  
         int recNum = returnRecNum(sb);  
         if (sb.toString().equals("false")) {  
             return false;  
         } else {  
             try {  
                 // 保存序列化的文件名默认为"serial_n.info"  
                 ObjectOutputStream oos = new ObjectOutputStream(  
                         new FileOutputStream(m_filePath + File.separator  
                                 + m_Str_Date + File.separator + m_Str_ID  
                                 + File.separator + "serial_" + (recNum + 1)  
                                 + ".info"));  
                 oos.writeObject(m_Obj.getStyledDocument());  
                 oos.close();  
                 // 表明当前记录个数为recNum+1;  
                 writeToRecNum(recNum + 1);  
             } catch (Exception e) {  
                 // TODO Auto-generated catch block  
                 JOptionPane.showMessageDialog(null, "保存记录时出错");  
                 return false;  
             }  
         } // else end  
         return true;  
     }  
     // 用这个返回的Object时，要记得强制转换为序列化前的类型。  
     public StyledDocument readFromFile() {  
         StyledDocument rtnObj;  
         StringBuffer sb = new StringBuffer("");  
         int recNum = returnRecNum(sb);  
         if (sb.toString().equals("false")) {  
             return null;  
         } else {  
             try {  
                 ObjectInputStream ois = new ObjectInputStream(  
                         new FileInputStream(m_filePath + File.separator  
                                 + m_Str_Date + File.separator + m_Str_ID  
                                 + File.separator + "serial_" + (recNum)  
                                 + ".info"));  
                 rtnObj = (StyledDocument) ois.readObject();  
                 ois.close();  
             } catch (Exception e) {  
                 // TODO Auto-generated catch block  
                 JOptionPane.showMessageDialog(null, "读取记录时出错");  
                 return null;  
             }  
         }  
         return rtnObj;  
     }  
     // 返回读取的记录序列化文件的个数的文件。返回个数。  
     // sb这个参数，传进来用于检测方法是否调用出错。sb需要初始化为空，即new StringBuffer("");  
     private int returnRecNum(StringBuffer sb) {  
         int num = 0;  
         File numFile;  
         // 默认这个文件名为RecordNum  
         numFile = new File(m_filePath + File.separator + m_Str_Date  
                 + File.separator + m_Str_ID + File.separator + "recordNum.txt");  
         if (!numFile.exists()) {  
             try {  
                 numFile.getParentFile().mkdirs();  
                 numFile.createNewFile();  
                 FileWriter fwriter = new FileWriter(numFile);  
                 fwriter.write("0");  
                 fwriter.close();  
             } catch (Exception e) {  
                 // TODO Auto-generated catch block  
                 JOptionPane.showMessageDialog(null, "创建记录文件失败");  
                 sb.append(false);  
             }  
             sb.append(true);  
             return 0;  
         } else {  
             try {  
                 FileReader fReader = new FileReader(numFile);  
                 BufferedReader bReader = new BufferedReader(fReader);  
                 num = Integer.valueOf(bReader.readLine());  
                 bReader.close();  
             } catch (Exception e) {  
                 // TODO: handle exception  
                 JOptionPane.showMessageDialog(null, "读取记录文件失败");  
                 sb.append(false);  
                 return 0;  
             }  
         }  
         sb.append(true);  
         return num;  
     }  
     private boolean writeToRecNum(int num) {  
         try {  
             File numFile = new File(m_filePath + File.separator + m_Str_Date  
                     + File.separator + m_Str_ID + File.separator  
                     + "recordNum.txt");  
             FileWriter fwriter = new FileWriter(numFile);  
             fwriter.write(String.valueOf(num));  
             fwriter.close();  
         } catch (Exception e) {  
             // TODO: handle exception  
             JOptionPane.showMessageDialog(null, "写入记录个数文件时出错");  
             return false;  
         }  
         return true;  
     }  
 }  